#include "zoneinstalui.h"
#include "ui_zoneinstalui.h"
#include <optrzone.h>
#include <optra/common/db.h>
#include <optra/common/proxy.h>
#include "zonesmanager.h"
#include <QSqlTableModel>
O_Z


extern Optra::Proxy* proxy;
extern ZonesManager* zmanager;

ZoneInstalUi::ZoneInstalUi(QWidget *parent) :
    GuiWindow(parent),
    ui(new Ui::ZoneInstalUi)
{
    ui->setupUi(this);

    connect(ui->installZoneBtn, SIGNAL(clicked()), SLOT(installClicked()));

}

ZoneInstalUi::~ZoneInstalUi()
{

    delete ui;
}


void ZoneInstalUi::setup()
{
    model_ = new QSqlTableModel(0, OZ->db()->db());
    model_->setTable("zones");
    model_->select();
    ui->zonesView->setModel(model_ );
}


void ZoneInstalUi::installClicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,tr("Open Zone"), proxy->directory(Optra::Proxy::zones).dirName(), tr("Zone Files (*.xml)"));
    QFile f(fileName);
    if (f.open(QFile::ReadOnly)){
        zmanager->installZone(&f);
    }
    model_->select();
}

void ZoneInstalUi::update()
{
    model_->select();
}

void ZoneInstalUi::beforeShow()
{

}

void ZoneInstalUi::beforeHide()
{

}

void ZoneInstalUi::remove()
{
    delete model_;
}

void ZoneInstalUi::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
